/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.vector.Vector3f;

/**
 *
 * @author Matt
 */
public class OrthoCamera extends Camera {

    float tx = 0;
    float ty = 0;
    float zoom = 20;
    float aspectRatio; //ratio of width to height
    int axis = 0;
    int width = 0;
    int height = 0;
    PVector target = new PVector();
    PVector up,right;

    public void setViewport() {
    }

    public OrthoCamera(float aspectRatio, int axis, int width, float x, float y, float z, int vx, int vy, int vw, int vh) {
        this.aspectRatio = aspectRatio;
        this.axis = axis;
        this.width = width;
        height = (int) (width / aspectRatio);

        pos = new PVector();

        this.pos.x = x;
        this.pos.y = y;
        this.pos.z = z;

        this.vx = vx;
        this.vy = vy;

        this.vw = vw;
        this.vh = vh;

        //I'm unfamiliar with the switch construct
        if (axis == 1) {
            up = new PVector(0, 1, 0);
            right = new PVector(0, 0, -1);

        } //top
        else if (axis == 2) {
            up = new PVector(0, 0, -1);
            right = new PVector(1, 0, 0);
        } //front
        else if (axis == 0) {
            up = new PVector(0, 1, 0);
            right = new PVector(1, 0, 0);
        }


    }

    public void camera() {

        GL11.glViewport(vx, vy, vw, vh);

        GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
        GL11.glLoadIdentity();

        // Calculate The Aspect Ratio Of The Window
        GL11.glOrtho(-zoom, zoom, -zoom / aspectRatio, zoom / aspectRatio, 0, 2 * FlightLifter.ZDIST);

        GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
        GL11.glLoadIdentity();

        GLU.gluLookAt(pos.x, pos.y, pos.z,
                target.x, target.y, target.z,
                0, 1, 0);

        GLProject.getMatrices();

    }

    public float screenToWorldX(int x) {
        x -= vx;



        float ret = (float) x / width * (2 * zoom) - zoom;

        if (axis == 1) {
            ret -= pos.z;
            // pos.y -= y;
        } //top
        else if (axis == 2) {
            ret += pos.x;
            // pos.z += y;

        } //front
        else if (axis == 0) {
            ret += pos.x;
            //   pos.y -= y;
        }


        return ret;
    }

    public float screenToWorldY(int y) {
        y -= vy;

        float ret = (float) y / height * (2 * (zoom / aspectRatio)) - (zoom / aspectRatio);

        if (axis == 1) {
            ret += pos.y;
            // pos.y -= y;
        } //top
        else if (axis == 2) {
            ret -= pos.z;
            // pos.z += y;

        } //front
        else if (axis == 0) {
            ret += pos.y;
            //   pos.y -= y;
        }

        return ret;
    }

    public PVector getUp() {
        return up.get();
    }
   public PVector getRight() {
        return right.get();
    }
    public Ray getRay(int mouseX, int mouseY) {
        /*

        
        GL11.glPushMatrix();
        camera();

        IntBuffer viewport = BufferUtils.createIntBuffer(16);
        FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
        FloatBuffer projection = BufferUtils.createFloatBuffer(16);
        FloatBuffer winZ = BufferUtils.createFloatBuffer(1);
        float winX, winY, wz;
        FloatBuffer position = BufferUtils.createFloatBuffer(3);

        GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
        GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
        GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);

        winX = (float) mouseX;
        winY = (float) mouseY;

        GL11.glReadPixels(mouseX, (int) winY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZ);
        wz = winZ.get();
        System.out.println(wz);
        GLU.gluUnProject(winX, winY, wz, modelview, projection, viewport, position);

        PVector dir = new PVector(position.get(0) - x, position.get(1) - y, position.get(2) - z);
        dir.normalize();

        GL11.glPopMatrix();
        return new Ray(new PVector(tx, ty, tz), dir,2*wz*FlightLifter.ZDIST);
         */

        float ox = screenToWorldX(mouseX);
        float oy = screenToWorldY(mouseY);
        PVector dir = null;

        float tx = pos.x, ty = pos.y, tz = pos.z;
        //right cam
        if (axis == 1) {
            tx = pos.x;
            ty = oy;
            tz = -ox;
            dir = new PVector(-1, 0, 0);
        } //top
        else if (axis == 2) {
            tx = ox;
            ty = pos.y;
            tz = -oy;
            dir = new PVector(0, -1, 0);
        } //front
        else if (axis == 0) {
            tx = ox;
            ty = oy;
            tz = pos.z;
            dir = new PVector(0, 0, -1);
        }
        return new Ray(new PVector(tx, ty, tz), dir, 100);

    }

    public void zoom(float amt) {
        zoom *= amt;
    }

    public void translate(float x, float y) {

        x *= .1f;
        y *= .1f;

        if (axis == 1) {

            pos.z += x;
            pos.y -= y;

            target.z += x;
            target.y -= y;
        } //top
        else if (axis == 2) {
            pos.x -= x;
            pos.z += y;

            target.x -= x;
            target.z += y;

        } //front
        else if (axis == 0) {
            pos.x -= x;
            pos.y -= y;

            target.x -= x;
            target.y -= y;

        }
    }
}
